package com.ruoyi.web.controller.byManager;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletResponse;

import com.alibaba.fastjson.JSONObject;
import com.ruoyi.system.domain.ByBsTime;
import com.ruoyi.system.service.IByBsTimeService;
import com.ruoyi.system.service.impl.SaveImgService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.ByBs;
import com.ruoyi.system.service.IByBsService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

@RestController
@RequestMapping("/by/bs")
public class ByBsController extends BaseController {

    @Autowired
    private IByBsService byBsService;

    @Autowired
    private SaveImgService saveImgService;

    @Autowired
    private IByBsTimeService byBsTimeService;


    @PostMapping("upBsStatus")
    public Object upBsStatus(@RequestBody String param){
        JSONObject json = JSONObject.parseObject(param);

        byBsService.upBsStatus(json);

        return AjaxResult.success();
    }


    @PostMapping("mangSubjectAndBsid")
    public Object mangSubjectAndBsid(@RequestBody String param){
        JSONObject json = JSONObject.parseObject(param);

        byBsService.mangSubjectAndBsid(json);

        return AjaxResult.success();
    }


    @PostMapping("saveImg")
    public Object saveImg(@RequestBody String param){
        JSONObject json = JSONObject.parseObject(param);
        String baseStr = json.getString("baseStr");
        if(baseStr.contains("data:image/jpeg;")){//进行保存jpg文件
            String accUrl = saveImgService.saveJpg(baseStr);
            return AjaxResult.success(accUrl);
        }else if(baseStr.contains("data:image/png;")){// 进行保存png文件
            String accUrl = saveImgService.savePng(baseStr);
            return AjaxResult.success(accUrl);
        }

        return AjaxResult.success();
    }


    /**
     * 查询帮手列表
     */
    @PreAuthorize("@ss.hasPermi('by:bs:list')")
    @GetMapping("/list")
    public TableDataInfo list(ByBs byBs)
    {
        startPage();
        List<ByBs> list = byBsService.selectByBsList(byBs);
        return getDataTable(list);
    }

    /**
     * 导出帮手列表
     */
    @PreAuthorize("@ss.hasPermi('by:bs:export')")
    @Log(title = "帮手", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, ByBs byBs)
    {
        List<ByBs> list = byBsService.selectByBsList(byBs);
        ExcelUtil<ByBs> util = new ExcelUtil<ByBs>(ByBs.class);
        util.exportExcel(response, list, "帮手数据");
    }

    /**
     * 获取帮手详细信息
     */
    @PreAuthorize("@ss.hasPermi('by:bs:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        Map map = new HashMap<>();
        ByBs bs = byBsService.selectByBsById(id);
        List<String> timeList = byBsTimeService.selectByBsTimeListNow(id.toString());
        map.put("bs", bs);
        map.put("timeList",timeList);
//        return success(byBsService.selectByBsById(id));
        return success(map);
    }

    /**
     * 新增帮手
     */
    @PreAuthorize("@ss.hasPermi('by:bs:add')")
    @Log(title = "帮手", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody ByBs byBs)
    {
        return toAjax(byBsService.insertByBs(byBs));
    }

    /**
     * 修改帮手
     */
    @PreAuthorize("@ss.hasPermi('by:bs:edit')")
    @Log(title = "帮手", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody ByBs byBs)
    {
        return toAjax(byBsService.updateByBs(byBs));
    }

    /**
     * 删除帮手
     */
    @PreAuthorize("@ss.hasPermi('by:bs:remove')")
    @Log(title = "帮手", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(byBsService.deleteByBsByIds(ids));
    }


}
